package xyc.sjxy.exam.utils;

import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtils {

    private  static DataSource ds=null; //这个属于dbcp数据源
    //private  static BasicDataSource ds=null; //这个属于dbcp数据源
   // private  static ComboPooledDataSource ds=null; //这个属于c3p0数据源
    //private  static DruidDataSource ds=null; //这个属于druid数据源
    static {
        try {
            Properties p=new Properties();
            //用类加载 器读取classpath路径下的配置文件jdbc.properties
            ClassLoader classLoader = JdbcUtils.class.getClassLoader();
            InputStream in = classLoader.getResourceAsStream("jdbc.properties");
            //以输入流的形式加载 jdbc.properties配置文件到properties的实例p中
            p.load(in);
            DruidDataSource bds = new DruidDataSource();
            bds.setDriverClassName(p.getProperty("jdbc.driver"));
            bds.setUrl(p.getProperty("jdbc.url"));
            bds.setUsername(p.getProperty("jdbc.username"));
            bds.setPassword(p.getProperty("jdbc.password"));
            ds=bds;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
        public static Connection getConnection() throws SQLException {
                return ds.getConnection();
        }
        public static DataSource getDataSource(){
        return ds;
    }
}
